#

#from InsightToolkit import *
import itk

from sys import argv
from math import pi

import drrInterpolator

#
# Read the fixed and moving images using filenames
# from the command line arguments
#
fixedImageReader = itk.ImageFileReader.IF2.New()
movingImageReader = itk.ImageFileReader.IF2.New()

fixedFileName = 'C:/Users/bryan/bryan-code/trunk/Tests/FixedImage.png'
movingFileName = 'C:/Users/bryan/bryan-code/trunk/Tests/MovingImage2.png'

fixedImageReader.SetFileName(  fixedFileName )
movingImageReader.SetFileName( movingFileName )

fixedImageReader.Update()
movingImageReader.Update()

fixedImage = fixedImageReader.GetOutput()
movingImage = movingImageReader.GetOutput()

#
#  Instantiate the classes for the registration framework
#
#registration    = itk.ImageRegistrationMethod.IF2IF2.New()
imageMetric     = itk.MeanSquaresImageToImageMetric.IF2IF2.New()
#imageMetric     = itk.MattesMutualInformationImageToImageMetric.IF2IF2.New()
#imageMetric     = itk.NormalizedCorrelationImageToImageMetric.IF2IF2.New()
#imageMetric     = itk.MeanReciprocalSquareDifferenceImageToImageMetric.IF2IF2.New()
#imageMetric     = itk.MutualInformationImageToImageMetric.IF2IF2.New()
transform       = itk.CenteredRigid2DTransform.D.New()
#optimizer       = itk.RegularStepGradientDescentOptimizer.New()
interpolator    = itk.LinearInterpolateImageFunction.IF2D.New()
#interpolator    = drrInterpolator.drrInterpolatorIF2D.New()

#
# Initial transform parameters
#
transform.SetAngle( 0.0 );

### center of the fixed image
##fixedSpacing = fixedImage.GetSpacing()
##fixedOrigin = fixedImage.GetOrigin()
##fixedSize = fixedImage.GetLargestPossibleRegion().GetSize()
##
##centerFixed = ( fixedOrigin.GetElement(0) + fixedSpacing.GetElement(0) * fixedSize.GetElement(0) / 2.0,
##                fixedOrigin.GetElement(1) + fixedSpacing.GetElement(1) * fixedSize.GetElement(1) / 2.0 )
##
##fixedImage.SetOrigin((0,0))
##
### center of the moving image
##movingSpacing = movingImage.GetSpacing()
##movingOrigin = movingImage.GetOrigin()
##movingSize = movingImage.GetLargestPossibleRegion().GetSize()
##
##centerMoving = ( movingOrigin.GetElement(0) + movingSpacing.GetElement(0) * movingSize.GetElement(0) / 2.0,
##                 movingOrigin.GetElement(1) + movingSpacing.GetElement(1) * movingSize.GetElement(1) / 2.0  )
##
##movingImage.SetOrigin((0,0))
##
### transform center
##center = transform.GetCenter()
##center.SetElement( 0, centerFixed[0] )
##center.SetElement( 1, centerFixed[1] )
##
###transform.SetCenter((256,256))

#
# Set up the metric and force it to bang out a value!
#

imageMetric.SetMovingImage(movingImage)
imageMetric.SetFixedImage(fixedImage)

imageMetric.SetTransform(transform.GetPointer())
imageMetric.SetInterpolator(interpolator.GetPointer())
imageMetric.DebugOn()

#itk.echo(imageMetric)

value = imageMetric.GetValue((0,256,256,0,0))
print value

caster = itk.CastImageFilter.IF2IUC2.New()
writer = itk.ImageFileWriter.IUC2.New()

caster.SetInput(movingImage)
writer.SetInput(caster.GetOutput())
writer.SetFileName('C:/Users/bryan/bryan-code/trunk/Tests/Test_ITKMetric_movingImage.png')
writer.Write()

##
##registration.SetOptimizer(      optimizer.GetPointer() )
##registration.SetTransform(      transform.GetPointer() )
##registration.SetInterpolator(   interpolator.GetPointer() )
##registration.SetMetric(         imageMetric.GetPointer() )
##registration.SetFixedImage(  fixedImage )
##registration.SetMovingImage( movingImage )
##registration.SetFixedImageRegion(  fixedImage.GetBufferedRegion() )
##
##

##
### transform translation
##translation = transform.GetTranslation()
##translation.SetElement( 0, centerMoving[0] - centerFixed[0] )
##translation.SetElement( 1, centerMoving[1] - centerFixed[1] )
##
##initialParameters = transform.GetParameters()
##
##print "Initial Parameters: "
##print "Angle: %f" % (initialParameters.GetElement(0), )
##print "Center: %f, %f" % ( initialParameters.GetElement(1), initialParameters.GetElement(2) )
##print "Translation: %f, %f" % (initialParameters.GetElement(3), initialParameters.GetElement(4))
##
##print "Interpolator: ", itk.echo(interpolator)
##
##registration.SetInitialTransformParameters( initialParameters )
##
###
### Define optimizer parameters
###
##
### optimizer scale
##translationScale = 1.0 / 100.0
##
##optimizerScales = itk.Array.D( transform.GetNumberOfParameters() )
##optimizerScales.SetElement(0, 1.0)
##optimizerScales.SetElement(1, translationScale)
##optimizerScales.SetElement(2, translationScale)
##optimizerScales.SetElement(3, translationScale)
##optimizerScales.SetElement(4, translationScale)
##
##optimizer.SetScales( optimizerScales )
##optimizer.SetMaximumStepLength( 0.75 )
##optimizer.SetMinimumStepLength( 0.005 )
##optimizer.SetNumberOfIterations( 250 )
##
###
### Iteration Observer
###
##def iterationUpdate():
##    currentParameter = transform.GetParameters()
##    print "N: %f  M: %f   P: %f %f %f %f %f " % (
##                                 optimizer.GetCurrentIteration(),
##                                 optimizer.GetValue(),
##                                 currentParameter.GetElement(0),
##                                 currentParameter.GetElement(1),
##                                 currentParameter.GetElement(2),
##                                 currentParameter.GetElement(3),
##                                 currentParameter.GetElement(4) )
##
##iterationCommand = itk.PyCommand.New()
##iterationCommand.SetCommandCallable( iterationUpdate )
##optimizer.AddObserver( itk.IterationEvent(), iterationCommand.GetPointer() )
##
##print "Starting registration"
##
###
### Start the registration process
###
##
##registration.StartRegistration()
